<template>
  <!-- 今日病人 -->
  <section>
    <table width="100%" border="0">
      <tr>
        <td width="460" style="vertical-align: top;padding-top:5px;">
          <el-button-group v-if="statisticArr.length>0">
            <el-button :type="form.checkStatus===''?'primary':''" @click="onGetDataStatus('')">全部</el-button>
            <el-button :type="form.checkStatus===0?'primary':''" @click="onGetDataStatus(0)">待检查({{statisticArr[1].count}}人)</el-button>
            <el-button :type="form.checkStatus===4?'primary':''" @click="onGetDataStatus(4)">检查中({{statisticArr[3].count}}人)</el-button>
            <el-button :type="form.checkStatus===1?'primary':''" @click="onGetDataStatus(1)">已完成({{statisticArr[2].count}}人)</el-button>
          </el-button-group>
        </td>
        <td style="text-align:right;">
          <el-form :inline="true" @submit.native.prevent="getData(-1)">
            <!-- <el-form-item>
              <el-select style="width:120px;" v-model="form.payStatus" clearable placeholder="收费状态">
                <el-option v-for="(item,index) in payStatusEnum" :key="index" :value="item.id" :label="item.desc"></el-option>
              </el-select>
            </el-form-item>  -->
            <el-form-item>
              <!-- <el-select  filterable remote clearable @clear="getPatient" :remote-method="getPatient" placeholder="患者姓名或手机号">
                <el-option v-for="(item,index) in patientList" :key="index" :value="item.patientId" :label="item.name+'--'+item.mobil"></el-option>
              </el-select> -->
              <patientSearch v-model="form.patientId"></patientSearch>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" circle icon="el-icon-search" native-type="submit"> </el-button>
            </el-form-item>
          </el-form>
        </td>
      </tr>
    </table>
    <el-row :gutter="20">
      <el-col v-for="(item,index) in tableList" :key="index" :xs="12" :sm="12" :md="12" :lg="8" :xl="6" class="reserve-user-list">
        <div :class="['card-box',classToName(item.checkStatusId)]">
          <div class="card-nr">
            <div>
              <img v-if="!item.headUrl" src="@/img/unknown-avatar.png" class="head-img" alt="" style="width:70px;height:70px;">
              <img v-else :src="item.headUrl|imageUrl" class="head-img" alt="" style="width:70px;height:70px;">
            </div>
            <div style="overflow: hidden;">
              <p class="overflow-omit">
                <span style="font-size:18px;">{{item.patientName}}</span>
                &nbsp;
                （{{item.sexDesc}}&nbsp;<span v-if="item.age>17">{{item.age}}岁</span><span v-else>{{item.birthday|toTimeContrast}}</span>）
              </p>
              <div class="overflow-omit">检验：{{item.checkDesc}}</div>
              <div class="overflow-omit">{{item.checkStatusDesc}} / {{item.payDesc}} / {{item.createTime|computeHour}}</div>
              <div class="overflow-omit">开单医生：{{item.doctorName}}（{{item.diagnosisTypeDesc}}）</div>
              <div class="overflow-omit">开单日期：{{item.createTime}}</div>
            </div>
          </div>
          <div class="card-footer">
            <div v-if="item.waitFor" @click="onCheckAffirem(item,0)">待检查({{item.waitFor}})</div>
            <div v-if="item.underWay" @click="onCheckAffirem(item,4)">检查中({{item.underWay}})</div>
            <!--   -->
            <div v-if="item.execute" @click="onComplete(item)">已完成({{item.execute}})</div>
          </div>
          <!-- <div class="zt-box">{{item.status|changeAssist(reserveTypeArray)}}</div> -->
          <div v-if="item.level" class="vip-box">
            <el-tooltip :content="item.levelName" placement="top">
              <img src="@/img/icon-vip.png" alt="">
            </el-tooltip>
          </div>
        </div>
      </el-col>
    </el-row>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br>
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <dialogCheckAffirm v-model="dialogCheckAffirm" :curItem="curItem" @success="onSuccess" @sign="onPrint"></dialogCheckAffirm>
    <iframe ref="iframe1" frameborder="0" height="0px" width="0px" src="/print/laboratoryLabel.html"></iframe>
  </section>
</template>
<script>
export default {
  props: ["qxBtns"],
  components: {
    dialogCheckAffirm: () => import("./_com/dialogCheckAffirm.vue"),
    patientSearch: () => import("@/pages/_com/patientSearch.vue")
  },
  data() {
    return {
      dialogCheckAffirm: false,
      curItem: null,
      form: {
        checkType: 0,
        checkStatus: "",
        payStatus: "",
        queryTime: "",
        patientId: ""
      },
      pager: { pageSize: 12, pageNum: 1, total: 0 },
      tableList: [],
      payStatusEnum: [],
      statisticArr: [],
      sampleEnum:[],
      reportUnitEnum:[],
      unitEnum:[],
      vesselEnum:[],
      vesselColorEnum:[]
    };
  },
  created() {
    let th = this;
    th.getCommonConfigByKey("支付状态").then(res => {
      th.payStatusEnum = res;
    });
    th.getCommonConfigByKey("实验室样本名称").then(res => {
      th.sampleEnum = res.reduce((prv,cur)=>{
         prv[cur.id]=cur.desc;
         return prv
      },{});
    });
    th.getCommonConfigByKey("单位").then(res => {
      th.unitEnum = res.reduce((prv,cur)=>{
         prv[cur.id]=cur.desc;
         return prv
      },{});
    });
    th.getCommonConfigByKey("实验室容器").then(res => {
      th.vesselEnum = res.reduce((prv,cur)=>{
         prv[cur.id]=cur.desc;
         return prv
      },{});
    });
    th.getCommonConfigByKey("试管颜色").then(res => {
      th.vesselColorEnum= res.reduce((prv,cur)=>{
         prv[cur.id]=cur.desc;
         return prv
      },{});
    });
    th.getData();
  },
  activated() {
    const th = this;
    th.getData(-1);
  },
  methods: {
    onComplete(obj) {
      const th = this;
      th.$router.push({
        path: "labPatientDetail",
        query: th.getUrlToken({
          cureId: th.jiaMi(obj.cureId),
          patientId: th.jiaMi(obj.patientId)
        })
      });
    },
    onSuccess(arr) {
      let th = this;
      th.getData();
      th.getStatistic();
      th.onPrint(arr);
    },
   async onPrint(arr) {
      if(arr){
        let userData=[];
        let objs=arr[0];
        if(typeof(objs) != 'object'){
          await this.$api.getPrintInformation({ids:arr.join(',')}).then(res=>{
              if(res.executed){
                userData=res.data;
              }
            })
        }else{
            userData=arr;
        }
        let j = {
                  list: userData,
                  user: this.curItem,
                  sampleEnum:this.sampleEnum,
                  reportUnitEnum:this.reportUnitEnum,
                  unitsEnum:this.unitEnum,
                  vesselEnum:this.vesselEnum,
                  vesselColorEnum:this.vesselColorEnum
        };
        this.$refs["iframe1"].contentWindow.postMessage(j,`${window.location.origin}/print/laboratoryLabel.html`);
      }
    }, 
    getStatistic() {
      let th = this;
      let j = {};
      Object.assign(j, th.form);
      delete j.checkStatus;
      th.$api.cureCheckupstatistictype(j).then(res => {
        if (res.executed) {
          th.statisticArr = res.data;
        }
      });
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {};
      Object.assign(j, th.form, th.pager);
      th.showLoading();
      th.$api.cureCheckuppageQueryV2(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
        th.showLoading(false);
      });
      th.getStatistic();
    },
    onGetDataStatus(type) {
      this.form.checkStatus = type;
      this.getData(-1);
    },
    onCheckAffirem(obj, type) {
      let th = this;
      if (!th.qxBtns.affirm) {
        th.$message.warning("权限不足");
        return;
      }
      obj.status = type;
      th.curItem = obj;
      th.dialogCheckAffirm = true;
    },
    classToName(type) {
      switch (type) {
        case 0:
          return "bg-dfz";
          break;
        case 1:
          return "bg-yjz";
          break;
        case 4:
          return "bg-sy";
          break;
      }
    }
  }
};
</script>
